package cn.xxd.xxdssoservice.service;

import cn.xxd.common.JsonUtils;
import cn.xxd.common.RedisConstants;
import cn.xxd.entity.LoginUser;
import cn.xxd.entity.ReturnT;
import cn.xxd.xxdssointerface.AuthorityService;
import cn.xxd.xxdssoservice.util.RedisUtil;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

@Service(version = "1.0.0", interfaceClass = AuthorityService.class)
public class AuthorityServiceImpl implements AuthorityService {

    @Autowired
    private RedisUtil redisUtil;
    @Value("${SESSION_EXPIRE}")
    private Integer SESSION_EXPIRE;

    @Override
    public ReturnT getUserByToken(String authority) {
        //根据authority到redis中取用户信息
        String json = redisUtil.get(authority, RedisConstants.datebase0);
        //取不到用户信息，登录已经过期，返回登录过期
        if (StringUtils.isBlank(json)) {
            return new ReturnT(400, "身份已过期");
        }
        //取到用户信息更新authority的过期时间
        redisUtil.expire(authority, SESSION_EXPIRE, RedisConstants.datebase0);
        //返回结果，E3Result其中包含TbUser对象
        LoginUser user = JsonUtils.jsonToPojo(json, LoginUser.class);
        return new ReturnT(user);
    }
}
